package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Utils;
import org.darkforest.dao.mapper.ShoppingGroupMapper;
import org.darkforest.dao.mapper.ShoppingGroupMemberMapper;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.ShoppingGroup;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

/**
 *
 * 拼团controller
 * Created by Administrator on 2017/2/28.
 */
@Controller
@RequestMapping(value = "promotion")
public class ShoppingGroupController {

    @Autowired
    private ShoppingGroupMapper shoppingGroupMapper;

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private ShoppingGroupMemberMapper shoppingGroupMemberMapper;


    @RequestMapping(value = "groupBuyList")
    public ModelAndView groupBuyList(@RequestParam(required = false, defaultValue = "") String activeName,
                                     @RequestParam(required = false, defaultValue = "1") String start,
                                     String activeState){
        ModelAndView res = new ModelAndView("promotion/group/shopping_group_list");

        ShoppingGroup shoppingGroup = new ShoppingGroup();
        shoppingGroup.setStart(Integer.parseInt(start));
        shoppingGroup.setActiveState(StringUtils.isBlank(activeState) ? null : Integer.parseInt(activeState));
        shoppingGroup.setActiveName(StringUtils.isBlank(activeName)?null:activeName);
        List<ShoppingGroup> shoppingGroupList = shoppingGroupMapper.selectWithGoodsByCondition(shoppingGroup);
        shoppingGroup.setResultList(shoppingGroupList);
        res.addObject("pageBean", shoppingGroup);
        res.addObject("activeState",activeState);
        return res;
    }



    @RequestMapping(value = "toAddShoppingBuy")
    public ModelAndView toAddShoppingBuy(){
        ModelAndView res = new ModelAndView("promotion/group/add_shopping_group");
        return res;
    }




    @RequestMapping(value = "shoppingBuyDetail")
    public ModelAndView shoppingBuyDetail(Integer activeId){
        ModelAndView res = new ModelAndView("promotion/group/shopping_group_detail");
        ShoppingGroup shoppingGroup = shoppingGroupMapper.selectWithGoodsByPrimaryKey(activeId);
        res.addObject("sg",shoppingGroup);
        return res;
    }


    @RequestMapping(value = "doAddShoppingBuy")
    public @ResponseBody AjaxReturnData doAddShoppingBuy(ShoppingGroup shoppingGroup){

        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        //状态进行中
        shoppingGroup.setActiveState(1);
        //中奖概率
        shoppingGroup.setActiveGiftRate(new BigDecimal(1).divide(new BigDecimal(shoppingGroup.getActiveMemberLimit()),4, BigDecimal.ROUND_HALF_EVEN).multiply(new BigDecimal(100)).setScale(2).floatValue());
        //新增活动时默认为第一轮
        shoppingGroup.setActiveCircleNum(1);
        shoppingGroup.setActiveMemberNum(0);
        //发布时间
        shoppingGroup.setActivePublishTime(DateUtil.getCurrentDateTime());

        try {
            shoppingGroupMapper.insertSelective(shoppingGroup);
            ajaxReturnData.setMessage("新增团购成功");
            ajaxReturnData.setResult(true);
        }catch (Exception e){
            e.printStackTrace();
            ajaxReturnData.setMessage("新增团购失败");
            ajaxReturnData.setResult(false);

        }


        return ajaxReturnData;
    }


    /**
     * 为添加优惠套餐选择商品
     * @param request
     * @param goodsName
     * @param start
     * @return
     */
    @RequestMapping(value = "chooseshopping_groupGoods")
    public ModelAndView chooseBundlingGoods(HttpServletRequest request,
                                            @RequestParam(required = false, defaultValue = "") String goodsName,
                                            @RequestParam(required = false, defaultValue = "1") String start
    ) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView("promotion/group/choose_shopping_group_goods");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

        GoodSearchBean goodSearchBean = new GoodSearchBean();
        goodSearchBean.setGoodsName(new String(goodsName.getBytes("ISO-8859-1"), "UTF-8"));
        goodSearchBean.setStoreId(sellerVo.getStoreId());
        goodSearchBean.setStart(Integer.parseInt(start));
        goodSearchBean.setPageSize(8);
        goodSearchBean.setGoodsType(0);
        goodSearchBean.setGoodsState(1);
        goodSearchBean.setGoodsVerify(1);
        this.goodsService.searchGoodsByPage(goodSearchBean);
        modelAndView.addObject("pageBean", goodSearchBean);
        return modelAndView;
    }




    @RequestMapping(value = "cancelActive")
    public @ResponseBody boolean cancelActive(Integer activeId){
        ShoppingGroup shoppingGroup = new ShoppingGroup();
        shoppingGroup.setActiveState(4);//取消状态
        shoppingGroup.setId(activeId);
        shoppingGroupMapper.updateByPrimaryKeySelective(shoppingGroup);

        return true;
    }


}
